/**
 * 检查app版本是否需要升级
 */
const app = getApp();
var urlPrefix = app.globalData.urlPrefix;
const checkVersion = ({
	name, //最新版本名称
	code, //最新版本号
	content, //更新内容
	url, //下载链接
	isForceUpdate, //是否强制升级
	isItProactive, //是否主动 1否 从首页/0是 从我的页面
}) => {
	const selfVersionCode = Number(uni.getSystemInfoSync().appVersionCode); //当前App版本号
	//线上版本号高于当前，进行在线升级
	if (code > selfVersionCode) {
		let platform = uni.getSystemInfoSync().platform //手机平台
		//安卓手机弹窗升级
		if (platform === 'android') {
                //当前页面不是升级页面跳转防止多次打开
			if (getCurrentPageRoute() !== 'pages/lq-upgrade/upgrade') {
				if(isItProactive == 1){
					var userId = uni.getStorageSync('userId');
					uni.request({
					    url: urlPrefix + '/api/app/version/isTheFuture?userId=' + userId + '&code=' + code,
					    method: 'GET',
					    header: {
							'token': uni.getStorageSync('token'),
					        'content-type': 'application/json'
					    },
					    success: function (res) {
					        if(!res.data.datas){
								uni.navigateTo({
									url: '/pages/lq-upgrade/upgrade?name='+name+"&content="+content+"&url="+url+"&isForceUpdate="+isForceUpdate+"&code="+code,
								})
							}
					    }
					});
				}else{
					uni.navigateTo({
						url: '/pages/lq-upgrade/upgrade?name='+name+"&content="+content+"&url="+url+"&isForceUpdate="+isForceUpdate+"&code="+code,
					})
				}
			}

		}
		//IOS无法在线升级提示到商店下载
		// else {
		// 	uni.showModal({
		// 		title: '发现新版本 ' + newVersionName,
		// 		content: '请到App store进行升级',
		// 		showCancel: false
		// 	})
		// }
	}else{
		return;
	}
}

//获取当前页面url
const getCurrentPageRoute = () => {
	let currentRoute;
	let pages = getCurrentPages() // 获取栈实例
	if (pages&&pages.length) {
		currentRoute = pages[pages.length - 1].route;

	}
	return currentRoute
}

export default checkVersion